package BaseTest

object SimpleWordCount {
  def main(args: Array[String]): Unit = {
    var lines = List("This is my name", "This is my girlfriend", "Hello,is it cute?", "yeah! Cool.")
    val res = for (line <- lines) yield line.split(" ")
    println(res)
    println(res.flatten)

    println("-" * 50)

    val res_1 = lines.map(x => x.split(" "))
    println(res_1)
    println(res_1.flatten)

    println("-" * 50)

    var res_2 = lines.flatMap(x => x.split(" ")) // map + flatten
    println(res_2)

    val resMap = res_2.map(x => (x, 1))
    val grouped: Map[String, List[(String, Int)]] = resMap.groupBy(t => t._1)
    println(grouped)
    println("-" * 50)
    println(grouped.map(t => (t._1, t._2.size)).toList.sortBy(t => t._2).reverse)
    println("-" * 50 + "一行实现")
    println(lines)
    //    println(lines.flatMap(l => l.split(" ")))
    val stringToInt = lines.flatMap(l => l.split(" ")).groupBy(x => x).mapValues(x => x.size)
    println(stringToInt.toList)

  }
}
